﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VSD.OneStopService.BLL;

public partial class Modules_TraCuuHoSo : System.Web.UI.Page
{
    private ProfileStoreRepository resProfileStore;
    List<ProfileStoreRepository.FTEProfileInfo> listProfiles;
    public int CurrentDeptID = -1;
    public int start = 0;
    int pageSize = 15;
    int pageSegSize = 1;
    int count = 0;

    public string searchAllProf
    {
        set;
        get;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        resProfileStore = new ProfileStoreRepository();
        txtSearchProfile.Focus();
        if (!IsPostBack)
        {

        }
    }

    public void Page_PreRender(object sender, EventArgs e)
    {
        FilterData();
    }

    public void FilterData()
    {
        try
        {
            resProfileStore = new ProfileStoreRepository();
            listProfiles = resProfileStore.GetFTEProfiles(start, pageSize, out count).ToList();
            searchAllProf = txtSearchProfile.Text.ToString().ToLower();

            if (listProfiles == null) return;

            if (string.IsNullOrWhiteSpace(searchAllProf))
            {
                this.rptHoSo.DataSource = listProfiles;
                rptHoSo.DataBind();
            }
            else
            {
                listProfiles = listProfiles.FindAll(x =>
                    (x.ServiceName != null && x.ServiceName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.SubscriberName != null && x.SubscriberName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.ProfileCode != null && x.ProfileCode.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.DeptName != null && x.DeptName.ToLower().Contains(searchAllProf.ToLower()))

                    ).ToList();

                this.rptHoSo.DataSource = listProfiles;
                rptHoSo.DataBind();
            }
        }
        catch (Exception)
        {
            //throw new 
        }

        AspNetPager1.RecordCount = count;
        AspNetPager1.PageSize = pageSize;
        // AspNetPager1.CurrentPageIndex = 1;

        if (AspNetPager1.PageCount <= 1) divPagination.Visible = false;
    }

    protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        FilterData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        txtSearchProfile.Focus();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        start = AspNetPager1.StartRecordIndex - 1;
        try
        {
            listProfiles = resProfileStore.GetFTEProfiles(start, pageSize, out count).ToList();
            searchAllProf = txtSearchProfile.Text.ToString().ToLower();
            if (listProfiles == null) return;

            if (string.IsNullOrWhiteSpace(searchAllProf))
            {
                this.rptHoSo.DataSource = listProfiles;
                rptHoSo.DataBind();
            }
            else
            {
                listProfiles = listProfiles.FindAll(x =>
                    (x.ServiceName != null && x.ServiceName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.SubscriberName != null && x.SubscriberName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.ProfileCode != null && x.ProfileCode.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.DeptName != null && x.DeptName.ToLower().Contains(searchAllProf.ToLower()))

                    ).ToList();

                this.rptHoSo.DataSource = listProfiles;
                rptHoSo.DataBind();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
}
//public int PageNumber
//{
//    get
//    {
//        if (ViewState["PageNumber"] != null)
//            return Convert.ToInt32(ViewState["PageNumber"]);
//        else
//            return 0;
//    }
//    set
//    {
//        ViewState["PageNumber"] = value;
//    }
//}
//PagedDataSource pgitems = new PagedDataSource();
//pgitems.DataSource = listProfiles;
//pgitems.AllowPaging = true;
//pgitems.PageSize = 10;
//pgitems.CurrentPageIndex = PageNumber;
//if (pgitems.PageCount > 1)
//{
//    rptPages.Visible = true;
//    System.Collections.ArrayList pages = new System.Collections.ArrayList();
//    for (int i = 0; i < pgitems.PageCount; i++)
//        pages.Add((i + 1).ToString());
//    rptPages.DataSource = pages;
//    rptPages.DataBind();
//}
//else
//    rptPages.Visible = false;